#ifndef _HEADERNAME_H
#define _HEADERNAME_H

#include<iostream>
#include<Winsock2.h>
#include<time.h>
#include<fstream>

#pragma comment(lib, "Ws2_32.lib")

using namespace std;
#endif // !_HEADERNAME_H


class TCP {
public:
	void loadTcp()
	{
		WORD wVersonRequired;
		WSADATA wsaData;
		wVersonRequired = MAKEWORD(1, 1);
		err = WSAStartup(wVersonRequired, &wsaData);
		if (err != 0) return;
	}

	void setTcp(const char *IP,int PORT)
	{
		sockCLient = socket(AF_INET, SOCK_STREAM, 0);
		addrSrv.sin_addr.S_un.S_addr = inet_addr(IP);
		addrSrv.sin_family = AF_INET;
		addrSrv.sin_port = htons(PORT);
	}

	void connectTcp()
	{
		status = connect(sockCLient, (SOCKADDR*)&addrSrv, sizeof(SOCKADDR));
		while (1) 
		{
			
			if (status == -1)
			{
				cout << "connect err!" << endl;
				cout << WSAGetLastError() << endl;
				status = connect(sockCLient, (SOCKADDR*)&addrSrv, sizeof(SOCKADDR));
				Sleep(1000);
			}
			else
			{
				int nRect = recv(sockCLient,RecvBuf,1024,0);
				if (nRect <= 0) {
					cout << WSAGetLastError() << endl;
				}
				else
				{
					cout << "Recv:" << RecvBuf << endl;
					memset(RecvBuf,0,sizeof(RecvBuf));
				}
			}
			
		}
	}

protected:
	struct sockaddr_in addrSrv;
	int sfd;
	int err;
	int status, save_errno;
	char RecvBuf[1024] = { 0 };
	SOCKET sockCLient = 0;
};
